#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdbool.h>
#include "listHandle.h"

#define LISTNUM  2
RECVLIST recvList[LISTNUM];

void initrecvList()
{
    memset(&recvList, 0, sizeof(struct tagRECVLIST) * LISTNUM);
    recvList[0].state = RECVING;
    recvList[1].state = IDEL;
    recvList[0].num = 0;
    recvList[1].num = 0;
}

__inline RECVLIST *getRecvingList()
{
    I32 i = 0;
    for ( i = 0; i < LISTNUM; i++ )
    {
        if ( recvList[i].state == RECVING )
        {
            return &recvList[i];
        }
    }

    for ( i = 0; i < LISTNUM; i++ )
    {
        if ( recvList[i].state == IDEL )
        {
            return &recvList[i];
        }
    }
    recvList[0].num = 0;
    return &recvList[0];
    
}

__inline RECVLIST *getUploadList()
{
    I32 i = 0;
    for ( i = 0; i < LISTNUM; i++)
    {
        if ( recvList[i].state == UPLOADING )
        {
            return &recvList[i]; 
        }
    }
    
    for ( i = 0; i < LISTNUM; i++)
    {
        if ( recvList[i].state == RECVING && recvList[i].num > 0 )
        {
            return &recvList[i];
        }
    }  
    return NULL;
}

__inline void setListState(RECVLIST *list, I32 state)
{
   list->state = state;
}

void resetList(RECVLIST *list)
{
    list->state = IDEL;
    list->num = 0;
}

__inline bool uploadingData()
{
    I32 i = 0;
    for ( i = 0; i < LISTNUM; i++)
    {
        if ( recvList[i].state == UPLOADING )
        {
            return true; 
        }
    }   
    return false;
}

__inline void resetUploadingList()
{
    I32 i = 0;
    for ( i = 0; i < LISTNUM; i++)
    {
        if ( recvList[i].state == UPLOADING )
        {
            resetList(&recvList[i]);
        }
    }   

}







